//
//  MainViewController.swift
//  sinaWeibo
//
//  Created by 史文杰 on 16/3/6.
//  Copyright © 2016年 swjie87. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        /*
        public var tabBar: UITabBar { get }  get是只读的
        swift中的系统默认的tabBar是只读的，所以不能直接进行设置，要把系统的tabBar替换掉，通过kvc方式替换掉
        */
        let mainTabBar = MainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        
        addChildViewControllers()
    }
    
    //MARK:加载控制器
    private func addChildViewControllers()
    {
        addViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addViewController(VC:UIViewController, title:String, imageName:String)
    {
        //只会设置tabBarItem对应的title
        //homeVC.tabBarItem.title = "首页"
        
        //会把tabBarItem 与 navVC的title设置为同一个title
        VC.title = title
        
        //设置字体颜色
        //VC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        tabBar.tintColor = UIColor.orangeColor()
        
        //设置 tintColor的颜色以后 字体颜色与选中图片的颜色会与tintColor的颜色一样
        //VC.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        
        VC.tabBarItem.image = UIImage(named: imageName)
        VC.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        let navVC = UINavigationController()
        navVC.addChildViewController(VC)
        
        //单独设置 navVC的title
        //navVC.navigationItem.title = "哈哈哈"
        navVC.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.orangeColor()]
        
        addChildViewController(navVC)
    }
    
}
